package com.it.controller;

import com.it.bean.MasterRepplayBean;
import com.it.bean.RepplyBean;
import com.it.bean.TopicBean;
import com.it.bean.UserBasicBean;
import com.it.service.MasterRepplyService;
import com.it.service.RepplyService;
import com.it.service.TopicServcie;
import com.it.service.UserService;
import com.it.utils.StringUtil;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

public class TopicController {
    public UserService userService;
    public TopicServcie topicServcie;
    public RepplyService repplyService;
    public MasterRepplyService masterRepplyService;


    public String delete(HttpServletRequest request) throws Exception {
        String id = request.getParameter("id");
        TopicBean topicBean = new TopicBean();
        topicBean.setId(Integer.parseInt(id));
        topicServcie.deleteTopic(topicBean);
        return "topic.do?operate=getTopicList";

    }

    public String getTopicList(HttpServletRequest request) throws Exception {
        UserBasicBean userBasicBean = (UserBasicBean) request.getSession().getAttribute("userBasicBean");
        String uid = request.getParameter("uid");
        if (StringUtil.isNotEmpty(uid)) {
            userBasicBean = userService.getUserBasicBeanById(uid);
        }
        List<TopicBean> topicList = topicServcie.getTopicListByUid(userBasicBean.getId());
        request.getSession().setAttribute("topicList", topicList);
        return "frames/right";

    }

    /**
     * 获取日志详情
     *
     * @param request
     * @return
     * @throws Exception
     */
    public String getTopicDetail(HttpServletRequest request) throws Exception {
        String topicId = request.getParameter("topicId");
        TopicBean topicBean = topicServcie.getTopicById(Integer.parseInt(topicId));
        topicBean.setUserBasicBean(userService.getUserBasicBeanById(String.valueOf(topicBean.getUid())));
        List<RepplyBean> repplyList = repplyService.getRepplyListByTopicId(topicId);
        for (int i = 0; i < repplyList.size(); i++) {
            repplyList.get(i).setUserBasicBean(userService.getUserBasicBeanById(String.valueOf(repplyList.get(i).getUid())));
            MasterRepplayBean masterRepplayBean = masterRepplyService.getMasterRepplyByReplyId( String.valueOf(repplyList.get(i).getId()));
            repplyList.get(i).setMasterRepplay(masterRepplayBean);

        }
        topicBean.setRepplyList(repplyList);
        request.setAttribute("topic", topicBean);
        return "frames/detail";

    }

}
